﻿using System;
using FellowshipOne.API;
using FellowshipOne.API.Base;
using MasVida.WebApi.Controllers;
using MasVida.WebApi.Models.Requests;
using MasVida.WebApi.Models.Responses;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Shouldly;

namespace MasVida.WebApi.Tests.Controllers
{
    [TestClass]
    public class LoginControllerTests
    {


        [TestMethod]
        public void TestValidateUser()
        {
            int expected = 60590973;
            Mock<IFellowshipOneClient> client = new Mock<IFellowshipOneClient>();
            var requestTicket = new F1OAuthTicket();
            var loginRequest = new LoginRequest()
            {
                LoginType = LoginType.PortalUser,
                Password = "UserName",
                Username = "Password"
            };
            client.Setup(c => c.GetRequestToken(It.IsAny<F1OAuthTicket>(), true, true)).Returns(requestTicket);
            var accessTicket = new F1OAuthTicket()
            {
                AccessTokenSecret = "token Secret",
                AccessToken = "Token",
                PersonURL = "https://masvida.staging.fellowshiponeapi.com/v1/People/" + expected
            };
            client.Setup(
                c =>
                    c.Authorize(requestTicket, loginRequest.Username, loginRequest.Password, loginRequest.LoginType,
                        true, true)).Returns(accessTicket);

            LoginController controller = new LoginController(client.Object);

            LoginResponse response = controller.Login(loginRequest);

            response.AccessTicket.AccessTokenSecret.ShouldBe(accessTicket.AccessTokenSecret);
            response.AccessTicket.AccessToken.ShouldBe(accessTicket.AccessToken);
            response.AccessTicket.PersonUrl.ShouldBe(accessTicket.PersonURL);
            response.PersonId.ShouldBe(expected);
            response.Success.ShouldBe(true);
        }

        [TestMethod]

        public void TestValidateUserThrowingException()
        {
            Mock<IFellowshipOneClient> client = new Mock<IFellowshipOneClient>();
            var loginRequest = new LoginRequest()
            {
                LoginType = LoginType.PortalUser,
                Password = "UserName",
                Username = "Password"
            };
            var message = "Invalid operation";
            client.Setup(c => c.GetRequestToken(It.IsAny<F1OAuthTicket>(), true, true)).Throws(new InvalidOperationException(message));
            
            LoginController controller = new LoginController(client.Object);

            LoginResponse response = controller.Login(loginRequest);

            Assert.IsNull(response.AccessTicket, "Access Ticket should be null");
            Assert.AreEqual(message, response.ErrorMessage, "Error message should be the same");
            response.Success.ShouldBe(false);
        }

        [TestMethod]

        public void TestValidateEmptyUser()
        {
            Mock<IFellowshipOneClient> client = new Mock<IFellowshipOneClient>();
            var loginRequest = new LoginRequest()
            {
                LoginType = LoginType.PortalUser,
                Password = "",
                Username = ""
            };

            LoginController controller = new LoginController(client.Object);

            LoginResponse response = controller.Login(loginRequest);

            response.AccessTicket.ShouldBe(null, "Access Ticket should be null");
            response.ErrorMessage.ShouldNotBeNullOrEmpty("Error message should not be null");
            response.Success.ShouldBe(false);
        }
    }
}
